<template>
    <div class="kickback_header">
        <div class="left">
            <div>可提现(元): <span>{{ Amountlist }}</span></div>
            <div>累计提现(元): <span>{{ AmountCount }}</span></div>
        </div>
        <div class="right" @click="onPayouts">
            立即提现
        </div>
    </div>
    
    <div class="kickback">
        <van-pull-refresh v-model="refreshing" @refresh="onRefresh" >
            <van-list
            v-model:loading="loading"
            :finished="finished"
            :offset="10"
            finished-text="没有更多了"
            @load="onLoad"
        >   
                <div class="item" v-for="(item,key) in commissionlist" :key="key">
                    <div class="left" >
                        <!-- <div class="img">
                            <img src="/assets/images/01.jpg" alt="">
                        </div> -->
                        <div class="content">
                            <div class="nickname">订单号：{{ item.order_code }}</div>
                            <div class="tel">商品名称：{{ item.product }}</div>
                        </div>
                    </div>
                    <div class="right">
                        <div class="status">{{ item.status_text }}</div>
                        <div class="money">+{{ item.amount }}</div>
                    </div>
                </div>
        </van-list>
        </van-pull-refresh>
    </div>

    <Tabbar />
</template>
<script setup>

import { ref } from 'vue';

    const {proxy} = getCurrentInstance()

    let business = proxy.$business
    // console.log(business)

    //刷新状态
    let refreshing = ref(false)

    //加载状态
    let loading = ref(false)

    //是否无更多数据
    let finished = ref(false)

    //当前页
    let page = ref(1)

    //列表数据
    let commissionlist = ref([])
    //可提现金额
    let Amountlist = ref('0.00')
    //累计提现金额
    let AmountCount = ref('0.00')
    // onMounted(() =>{
    //     CommissionMoeny()
    // })

    //下拉刷新
    const onRefresh = async () => {
        // console.log('下拉刷新')
        //重置当前页
        page.value = 1
        //重置列表数据
        commissionlist.value = []
        //重置加载状态
        loading.value = true
        //重置加载完成状态
        finished.value = false
        //重新获取数据
        onLoad()
    }
    const onLoad = async() => {

        //判断是否为加载中的状态
        if(refreshing.value)
        {
            refreshing.value = false
        }
        var params = {
            busid: business.value.id,
            page:page.value
        }

        var result = await proxy.$POST({
            url: '/sale/commission',
            params,
        })

        loading.value = false
        // console.log(result)
        // return false
        if(result.code == 0)
        {
            finished.value = true
            return false
        }

        //因为有三个数据所以是选择第三个数据result.data.Commissionlist 后端传回什么字段就是什么字段区分大小写
        commissionlist.value = commissionlist.value.concat(result.data.Commissionlist)
        page.value++ 
        // return false
        // commissionlist.value = result.data.Commission
        Amountlist.value = result.data.Amountlist.toFixed(2)
        AmountCount.value = result.data.AmountCount.toFixed(2)
        // console.log(AmountCount.value)

    }

    const onPayouts = async () => 
    {
        proxy.$confirm({
            title: '确认提现',
            content: '是否提现全部佣金',
        }).then(async () => {
            var params = {
            busid: business.value.id
            }
            var result = await proxy.$POST({
                url: '/sale/payouts',
                params,
            })

            // console.log(result)
            // return false
            if(result.code === 1)
            {
                proxy.$success({
                    message: result.msg,
                    onClose: () => 
                    {
                        //重新计算佣金
                        var amount = parseFloat(result.data.AmountCount) + parseFloat(AmountCount.value)

                        //重新赋值
                        AmountCount.value = amount.toFixed(2)
                        // console.log(AmountCount.value)
                        Amountlist.value = '0.00'
                        // commissionlist.value = []
                        //重新刷新数据
                        onRefresh()

                        return false
                    }
                })
            }else
            {
                proxy.$fail(result.msg)
                return false
            }
        }).catch((error) => {})
        // OnRefresh()
    }



</script>

<style scoped>
    @import url('/assets/css/SaleMoney.css');
</style>